package org.lanqiao.oa.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.apache.ibatis.executor.ResultExtractor;
import org.lanqiao.oa.mapper.PaginationCondition;
import org.lanqiao.oa.mapper.ResultInfo;
import org.lanqiao.oa.mapper.User;
import org.lanqiao.oa.service.PermissionService;
import org.lanqiao.oa.service.UserService;
import org.lanqiao.oa.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/userController")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 跳转页面
     * @param pagePath
     * @return
     */
    @RequestMapping("/forwordPage")
    public String forwordPage(String pagePath, ModelMap modelMap){
        return "forward:/WEB-INF/qx/"+pagePath;
    }

    /**
     * 分页查询用户
     * @param page
     * @return
     */
    @RequestMapping("/getUserPage")
    @ResponseBody
    public Page<User> getUserPage(Page page){
        List<User> byPage = userService.findByPage(page);
        page.setRecords(byPage);
        return page;
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @RequestMapping("/saveUser")
    @ResponseBody
    public ResultInfo saveUser(User user){
        return ResultInfo.getResultInfo(userService.saveUser(user));
    }

    /**
     * 获取角色跳转查看明细页面
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/toUserParticulars")
    public String toUserParticulars(String id,String url, ModelMap modelMap){
        User user = userService.selectById(id);
        modelMap.put("user",user);
        return "forward:/WEB-INF/qx/user/"+url+".jsp";
    }

    /**
     * 查询用户权限树
     * @param id
     * @return
     */
    @RequestMapping("/getUserPermissionById")
    @ResponseBody
    public List<PermissionVo> getUserPermissionById(String id){
        return userService.getUserPermissionById(id);
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @RequestMapping("/deleteUserByUid")
    @ResponseBody
    public ResultInfo deleteUserByUid(String uid){
        return ResultInfo.getResultInfo(userService.deleteUserByUid(uid));
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public ResultInfo updateUser(User user){
        return ResultInfo.getResultInfo(userService.updateUserById(user));
    }

    /**
     * 修改用户状态
     * @param uid
     * @param status
     * @return
     */
    @RequestMapping("/updatelockStatus")
    @ResponseBody
    public ResultInfo updatelockStatus(String uid,Integer status){
        return ResultInfo.getResultInfo(userService.updatelockStatus(uid,status));
    }

    /**
     * 根据条件查询用户
     * @return
     */
    @RequestMapping("/selectUserPage")
    @ResponseBody
    public PaginationCondition<User> selectUserPage(PaginationCondition condition){
        System.out.println(condition);
        return userService.selectUserByCondition(condition);
    }

    /**
     * 获取用户分配以及未分配的角色
     * @param userId
     * @return
     */
    @RequestMapping("/getAssignRole")
    @ResponseBody
    public Map getAssignRole(String userId){
        return userService.getAssignRole(userId);
    }

    /**
     * 给用户分配角色
     * @param uid
     * @param rid
     * @return
     */
    @RequestMapping("/assignRole")
    @ResponseBody
    public ResultInfo assignRole(String uid,String rid){
        return ResultInfo.getResultInfo(userService.assignRole(uid,rid));
    }

    /**
     * 删除用户的角色
     * @param uid
     * @param rid
     * @return
     */
    @RequestMapping("/unAssignRole")
    @ResponseBody
    public ResultInfo unAssignRole(String uid,String rid){
        return ResultInfo.getResultInfo(userService.unAssignRole(uid,rid));
    }

}
